﻿package core.object {
	
	import core.ColorMatrix;
	import core.DisplayUtil;
	import core.GameContainer;
	import core.manager.SpriteSheetManager;
	
	import events.GameAnimationEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;
	
	public class GameBall extends Sprite {
		
		private var _speedY:Number;
		private var _tallness:Number;
		private var _process:int;
		private var _throwed:Boolean;
		private var _shadow:Shape;
		private var _line:int;
		private var _kind:int;
		private var _angle:Number;
		private var starTimer:Timer;
		private var _targetX:Number;
		private var _targetY:Number;
		private var _onRoad:Boolean;
		private var _across:int;
		
		public static const toAngle:Number = 57.2957795130823;
		public static const toRadian:Number = 0.0174532925199433;
		public static const radius:Number = 13;
		
		private var animation:GameAnimation;
		
		private var _id:int;
		
		public function GameBall(id:int = 1):void{
			this._id = id;
			animation = new GameAnimation(id);
			animation.addEventListener(GameAnimationEvent.REDRAW_EVENT,redraw);
			this.addChild(animation);
			this.addEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
		}
		
		public function get id():int {
			return this._id;
		}
		
		public function set id(value:int):void {
			this._id = value;
			this.animation.id = value;
		}
		
		private var light:GameAnimation;
		
		private function addToStageHandler(event:Event):void{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.addToStageHandler);
			this.mouseEnabled = false;
			this.mouseChildren = false;
			this._process = 0;
			this.across = 0;
			this._tallness = 0;
			this.light = new GameAnimation(1002,false);
			this.addChild(light);
			this._shadow = new Shape();
			this._shadow.graphics.beginFill(0, 0.3);
			this._shadow.graphics.drawCircle(8, 8, radius);
			this._shadow.graphics.endFill();
			this.addChildAt(this._shadow,0);
		}
		
		public function redraw(event:GameAnimationEvent):void {
			this.rotation = ((this._angle * toAngle) - 90);
		}
		
		public function set onRoad(value:Boolean):void{
			this._onRoad = value;
		}
		public function get onRoad():Boolean{
			return this._onRoad;
		}
		
		public function set line(value:int):void{
			this._line = value;
		}
		
		public function get line():int{
			return this._line;
		}
		
		public function set angle(value:Number):void{
			this._angle = value;
		}
		
		public function get angle():Number{
			return (this._angle);
		}
		
		public function set across(value:int):void{
			this._across = value;
		}
		
		public function get across():int{
			return (this._across);
		}
		
		public function set process(value:Number):void{
			if (value > (GameContainer.MapData[this._line].length - 1)){
				value = (GameContainer.MapData[this._line].length - 1);
			}
			this._process = value;
			this.showProcess();
		}
		
		public function get process():Number{
			return this._process;
		}
		
		public function set tallness(value:Number):void{
			this._tallness = value;
		}
		public function get tallness():Number{
			return (this._tallness);
		}
		public function set speedY(_arg1:Number):void{
			this._speedY = _arg1;
		}
		public function get speedY():Number{
			return (this._speedY);
		}
		public function set throwed(_arg1:Boolean):void{
			this._throwed = _arg1;
		}
		public function get throwed():Boolean{
			return (this._throwed);
		}
		public function set targetX(_arg1:Number):void{
			this._targetX = _arg1;
		}
		public function get targetX():Number{
			return (this._targetX);
		}
		public function set targetY(_arg1:Number):void{
			this._targetY = _arg1;
		}
		public function get targetY():Number{
			return (this._targetY);
		}
		
		private function showProcess():void{
			if (!this._onRoad || (this._process < 0)){
				return;
			}
			var list:Array = GameContainer.MapData[this._line][this._process];
			this.x = list[0];
			this.y = list[1];
			this._angle = list[2];
		}
		
		private var bomb:GameAnimation;
		
		public function explode(id:int = 0):void{
			bomb = new GameAnimation(1000);
			bomb.func = this.diposeBomb;
			bomb.loop = false;// TODO 位置有些问题,需要调整
			this.addChild(bomb);
			DisplayUtil.removeChild(this._shadow);
			this.animation.dispose();
		}
		
		public function diposeBomb():void {
			this.bomb.dispose();
			this.dispose();
		}
		
		public function dispose():void {
			animation.removeEventListener(GameAnimationEvent.REDRAW_EVENT,redraw);
			animation.dispose();
			DisplayUtil.removeChild(this);
		}
		
		public function clone():GameBall {
			var ball:GameBall = new GameBall(this.animation.id);
			return ball;
		}
		
		private var _highlight:Boolean;
		
		public function set highlight(value:Boolean):void {
			if(this._highlight != value){
				this._highlight = value;
				if(value){
					this.animation.filters = [new GlowFilter(0xFF99AA)];// 发光的效果需要设置一下
				}else {
					this.animation.filters = null;
				}
			}
		}
		
		public var lineID:int = -1;
		
		public function get isProp():Boolean {
			return false;
		}
	}
}
